local filelog = require "filelog"

require "bjlenum"

-------------------------

local CardTool = { }

local CardColor = {
	diamonds = 1,	--方块
	clubs = 2,		--梅花
	hearts = 3,		--红桃
	spades = 4,		--黑桃
}

-----------------------------------

--转换牌数据
function CardTool.convert_cards(cards)
	local player_cards = {}
	local banker_cards = {}
	
	for _,carditem in ipairs(cards) do
		if carditem.status == "open" then
			local ct = CardColor[carditem.suit]
			local cv = tonumber(carditem.point)
			local card = ct * 100 + cv
			local idx = tonumber(carditem.index)
			if carditem.type == "player" then
				table.insert(player_cards, card)
			elseif carditem.type == "banker" then
				table.insert(banker_cards, card)
			end
		else
			break
		end
	end
	
	local allcards = {
		banker_cards = banker_cards,
		player_cards = player_cards,
	}
	
	return allcards
end

--获得开牌结果
function CardTool.get_opencardresult(gameResult)
	local is_fillcard = false
	
	--转换牌数据
	local allcards = CardTool.convert_cards(gameResult.cards)
	
	--牌数为2，4，5，6才通知
	local card_num = #allcards.player_cards + #allcards.player_cards
	if card_num < 2 or card_num == 3 or card_num > 6 then
		return false
	end
	
	--是否补牌
	if #allcards.banker_cards > 2 or #allcards.player_cards > 2 then
		is_fillcard = true
	end
	
	local result_list = {
		banker_cards = allcards.banker_cards,
		player_cards = allcards.player_cards,
		is_fillcard = is_fillcard,
	}
	
	return result_list
end

--获得游戏结果
function CardTool.get_gameresult(gameResult)
	local prize_id = nil
	local is_fillcard = false
	local ext_prize_list = {}
	
	local banker_points = tonumber(gameResult.bankerPoint)
	local player_points = tonumber(gameResult.playerPoint)
	if banker_points > player_points then
		prize_id = EBJLBetBtnId.BET_BTN_ID_BANKER
	elseif banker_points < player_points then
		prize_id = EBJLBetBtnId.BET_BTN_ID_PLAYER
	else
		prize_id = EBJLBetBtnId.BET_BTN_ID_TIE
	end
	
	--闲对
	local playerPair = tonumber(gameResult.playerPair) or 0
	if playerPair > 0 then
		table.insert(ext_prize_list, EBJLBetBtnId.BET_BTN_ID_PLAYER_PAIR)
	end
	
	--庄对
	local bankerPair = tonumber(gameResult.bankerPair) or 0
	if bankerPair > 0 then
		table.insert(ext_prize_list, EBJLBetBtnId.BET_BTN_ID_BANKER_PAIR)
	end
	
	--转换牌数据
	local allcards = CardTool.convert_cards(gameResult.cards)
	
	--是否补牌
	if #allcards.banker_cards > 2 or #allcards.player_cards > 2 then
		is_fillcard = true
	end
	
	local result_list = {
		banker_cards = allcards.banker_cards,
		player_cards = allcards.player_cards,
		banker_points = banker_points,
		player_points = player_points,
		prize_id = prize_id,
		ext_prize_list = ext_prize_list,
		is_fillcard = is_fillcard,
	}
	
	return result_list
end

return CardTool
